package com.imooc.bilibili.api;

import com.imooc.bilibili.domain.core.JsonResponse;
import com.imooc.bilibili.domain.video.Video;
import com.imooc.bilibili.elasticsearch.ElasticSearchService;
import com.imooc.bilibili.feign.MsDeclareService;
import com.imooc.bilibili.util.FastDFSUtil;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCollapser;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author gcq
 * @Create 2022-06-04
 */
@RestController
public class DemoApi {

    @Autowired
    FastDFSUtil fastDFSUtil;

    @Autowired
    private MsDeclareService msDeclareService;

//    @Autowired
//    private ElasticSearchService elasticSearchService;

    @GetMapping("/slices")
    public void slices(MultipartFile file) throws IOException {
        fastDFSUtil.convertFileToSlices(file);
    }

    //    @GetMapping("/es-videos")
//    public JsonResponse<Video> getEsVideo(@RequestParam String keyword) {
//        Video video = elasticSearchService.getVideo(keyword);
//        return new JsonResponse<>(video);
//    }

    @GetMapping("/demos")
    public Long msGet(@RequestParam Long id) {
        return msDeclareService.msGet(id);
    }

    public Map<String, Object> error(Map<String, Object> params) {
        Map<String, Object> map = new HashMap<>();
        map.put("error", "超时");
        return map;
    }

    @HystrixCommand(fallbackMethod = "error",
            commandProperties = {
                    @HystrixProperty(
                            name = "execution.isolation.thread.timeoutInMilliseconds",
                            value = "2000"
                    )
            }
    )
    @PostMapping("/demos")
    public Map<String, Object> msPost(@RequestBody Map<String, Object> params) {
        return msDeclareService.msPost(params);
    }
}